<script setup>
import EnvConfig from '@core/utils/EnvConfig';
import ThirdPlatformSession from '@core/engine/ThirdPlatform/Session';
import Session from '@core/engine/Session';
import HistoryHelper from '@core/utils/HistoryHelper';

const { t } = useI18n({ inheritLocale: true, useScope: 'local' });

const isConnectTo3rdPlatform = EnvConfig.isConnectTo3rdPlatform();
const username = isConnectTo3rdPlatform ? ThirdPlatformSession.getUsername() : Session.getUsername();

const onClickUserManagement = () => {
  HistoryHelper.replace('/userManagement');
};
const onClickExit = () => {
  if (isConnectTo3rdPlatform) {
    ThirdPlatformSession.logout();
  } else {
    Session.logout();
  }
};
</script>

<template>
  <ElDropdown>
    <div class="user-menu">
      <span>{{username}}</span>
    </div>
    <template #dropdown>
      <!-- TODO: 修改下拉菜单样式 -->
      <ElDropdownMenu>
        <ElDropdownItem v-if="isConnectTo3rdPlatform" @click="onClickUserManagement">
          {{t('userManagement')}}
        </ElDropdownItem>
        <ElDropdownItem @click="onClickExit">{{t('exit')}}</ElDropdownItem>
      </ElDropdownMenu>
    </template>
  </ElDropdown>
</template>

<i18n lang="yaml" locale="zh-CN">
  userManagement: '用户管理'
  exit: '退出'
</i18n>

<style lang="scss" scoped>
.user-menu {
  display: flex;
  flex-direction: row;
  align-items: center;
  height: 50px;
  margin-right: 16px;
  font-size: 12px;
  color: #c2c2c2;

  &:hover {
    cursor: pointer;
  }

  .icon-avatar {
    width: 24px;
    height: 24px;
    margin: 0 8px 0 0;
    color: #fff;
    background-color: #ccc;
    border-radius: 24px;
  }
}
</style>
